/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','opinionRecord_edit');

/*controller部分*/

//主要操作controller
authApp.controller("opinionEditController",function($scope,$http,$getQueryString,$timeout,$sce){
	$scope.schoolId=$getQueryString("schoolId");
	$scope.itemId=$getQueryString("itemId");
	$scope.number=$getQueryString("number");
	$scope.state=$getQueryString("state");
	$scope.isSecond="";
	try{
		$scope.isSecond=$getQueryString("isSecond");
		var li = document.getElementById("material_manage");   
		li.setAttribute("class", "choosen");
	}catch(msg){}
	$scope.backTarget="opinion_record.html";
	//**********主体方法
	$scope.ifFirstTimeQuery=true;
	$scope.isEdit=true;
	$scope.exTitleInfo="分项意见记录表";
	if($scope.state=="0"){
		$scope.titleInfo="填写";
	}else if($scope.state=="1"){
		$scope.titleInfo="修改";
	}else if($scope.state=="2"){
		//$scope.exTitleInfo="分项意见记录表列表";
		$scope.titleInfo="查看";
		$scope.isEdit=false;
		//$scope.backTarget="opinion_summary.html";
	}
	var queryMainApi=apis.queryAuthItem;	
	var requestJson={'schoolId':$scope.schoolId,'itemId':$scope.itemId,'state':$scope.state};	
	$scope.queryMain=function(){
		$http({
			method:'get',
			url:queryMainApi,
			params:requestJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	if(undefined==$scope.isSecond){
		var btarget=$scope.backTarget.split(".");
		var li = document.getElementById(btarget[0]);  
		li.setAttribute("class", "choosen");
	}
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		if(response.code=="000000"){
			$scope.list=response.data;
			
			angular.forEach($scope.list,function(data,index,array){
				if(data){
					data.num=$scope.number+"-"+data.number;
					var t=data.number;
					angular.forEach(data.list,function(data,index,array){
						data.num=$scope.number+"."+t+"."+data.number;
					})
				}
			})
		}else{
			alert(response.errorMsg);
		}
	}
	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */
	
	//根据细则值计算二级指标等级
	$scope.showResult=function(){
		angular.forEach($scope.list,function(data,index,array){
			var anum=0;
			var bnum=0;
			var cnum=0;
			var count=0;//细则空值数量
			angular.forEach(data.list,function(data,index,array){
				switch(data.level){
					case"A":anum++;
					break;
					case"B":bnum++;
					break;
					case"C":cnum++;
					break;
					default:count++;
				}
			})
			if(data.list.length>0){
				if(count>0)
					data.level="";
				else{ 
					if(bnum==0 && cnum==0)
						data.level="A";
					else if(bnum>0 && cnum==0)
						data.level="B";
					else if(cnum>0)
						data.level="C";
				}
			}
		})
	}
	
	//表格数据重置
	$scope.resetAll=function(){
		if(window.confirm("是否确认重置数据？")){
			angular.forEach($scope.list,function(data,index,array){
				angular.forEach(data.list,function(data,index,array){
					data.level="";
				})
				$scope.showResult();
			})
		}
	}
	
	//返回到前页面
	$scope.goback=function(){
		if(window.confirm("是否确认返回？")){
			javascript:history.back(-1);
		}
	}
	
	//表格数据保存
	$scope.saveAll=function(){
		if(window.confirm("是否确认保存数据？")){
			var isAllow=true;
			var detailLevel="";
			var twoLevel="";
			var mainProblem="";
			
			angular.forEach($scope.list,function(data,index,array){
				if(data.level==""||(data.level!="A"&&data.mainProblem==""))
					isAllow=false;
				else{
					twoLevel+=data.level+"#";
					if(data.mainProblem)
						mainProblem+=data.mainProblem+"#";
					else
						mainProblem+=" #";
					angular.forEach(data.list,function(data,index,array){
						detailLevel+=data.level+",";
					})
					detailLevel+="#";
				}
			})
			
			if(isAllow){
				var data={'schoolId':$scope.schoolId,'itemId':$scope.itemId,'detailLevel':detailLevel,'twoLevel':twoLevel,'mainProblem':mainProblem};
				$http({
					url:apis.saveAuthExpertItemData,
					method:"post",
					data:data,
				}).success(function(response){
					if(response.code=="000000"){
						alert("保存数据成功！");
						location.href="opinion_record.html";
					}
				})
			}else{
				alert("信息未填充完整");
			}
		}
	}
	
	//表格数据提交
	$scope.submitAll=function(){
		if(window.confirm("是否确认提交数据？提交后将无法修改！")){
			var isAllow=true;
			var detailLevel="";
			var twoLevel="";
			var mainProblem="";
			
			angular.forEach($scope.list,function(data,index,array){
				if(data.level==""||(data.level!="A"&&data.mainProblem==""))
					isAllow=false;
				else{
					twoLevel+=data.level+"#";
					if(data.mainProblem)
						mainProblem+=data.mainProblem+"#";
					else
						mainProblem+=" #";
					angular.forEach(data.list,function(data,index,array){
						detailLevel+=data.level+",";
					})
					detailLevel+="#";
				}
			})
			
			if(isAllow){
				var data={'schoolId':$scope.schoolId,'itemId':$scope.itemId,'detailLevel':detailLevel,'twoLevel':twoLevel,'mainProblem':mainProblem};
				$http({
					url:apis.saveAuthExpertItemDataTable,
					method:"post",
					data:data,
				}).success(function(response){
					if(response.code=="000000"){
						alert("提交数据成功！");
						location.href="opinion_record.html";
					}
				})
			}else{
				alert("信息未填充完整");
			}
		}
	}
})
